<?php
namespace app\api\controller;

use think\Cache;
use think\Db;
use app\api\controller\Base;
use app\api\controller\Message;
use think\Session;
use think\Request;

class Xinxi extends Base
{
    //商圈的显示页面
    public function index()
    {
        $openid=input('openid');
        $data = Db::name('user')->where('code',$openid)->find();
        $uid=$data['uid'];
        $page = input('page');
        $attention = Db::name("fans")->where("f_fans.uid",$uid)->field('guid')->find();
        $str=$attention['guid'].$uid;
        $where['me.uid']=array("in",$str);
        //print_r($where);die;
        $where['me.mflg']=1;
        $where['me.stoptime']=array("egt",date("Y-m-d H:i:s"));
        $ziji['me.uid']=$uid;
        $ziji['me.mflg']=1;
        $ziji['me.stoptime']=array("egt",date("Y-m-d H:i:s"));
        //未关注别人的时候查询我有没有发布文章
        $list = Db::name("message")
            ->alias('me')
            ->join('f_real re','re.uid=me.uid')
            ->join('f_user us','us.uid=re.uid')
            ->where($ziji)
            ->select();
        //return count($list);die;
        //获取当前域名
        $request = Request::instance();
        $domain=$request->domain();
        if(!empty($attention['guid']))
        {
            //print_r($where);die;
            $list = Db::name("message")
                ->alias('me')
                ->join('f_real re','re.uid=me.uid')
                ->join('f_user us','us.uid=re.uid')
                ->where($where)
                //->whereOr($ziji)
                ->order("me.createtime desc")
                ->page($page,'47')
                ->field('me.mid,me.type,me.price,us.uid,me.number,me.unit,me.address,us.nickname,us.uname,me.createtime,me.picture,me.title,re.rflg,me.faxian,me.content,us.gflg,me.mflg,me.stoptime')->select();
            //print_r($list);die;
            //地址
            $url=$domain."/upload/message/";
                foreach ($list as $key=>$value)
                {
                    if($value['gflg']=="1")
                    {
                        //return $key[''];
                        $list[$key]['nickname']='农厂汇官网';
                    }
                    elseif($value['uname']=='')
                    {
                        $list[$key]['nickname']=$list[$key]['nickname'];
                    }
                    else
                    {
                        $list[$key]['nickname']=$value['uname'];
                    }
                    if($value['unit']=="1")
                    {
                        $list[$key]['unit']='吨';
                    }
                    elseif($value['unit']=="2")
                    {
                        $list[$key]['unit']='斤';
                    }
                    elseif($value['unit']=="3")
                    {
                        $list[$key]['unit']='斤';
                    }
                    else
                    {
                        $list[$key]['unit']='个';
                    }
                    $a=$list[$key]['picture'];
                    $arr=array_filter(explode(',',$a));
                    //print_r($arr);die;
                    //数组非空设置追加路径
                    if(!empty($arr))
                    {
                        $list[$key]['picture']=$url.$arr[0];
                    }
                    else
                    {
                        $list[$key]['picture']=$url.'no.jpg';
                    }
                }
//            print_r($list[$key]['picture']);die;
            return $this->AjaxReturn("1","操作成功",$list);
        }
        elseif(count($list)>0)
        {
            // print_r($list[$key]['picture']);die;
            $list = Db::name("message")
                ->alias('me')
                ->join('f_real re','re.uid=me.uid')
                ->join('f_user us','us.uid=re.uid')
                ->where($ziji)
                ->order("me.createtime desc")
                ->page($page,'7')
                ->field('me.mid,me.type,me.price,us.uid,me.number,me.unit,me.address,us.uname,us.nickname,me.createtime,me.picture,me.title,re.rflg,me.faxian,me.content,us.gflg,me.mflg,me.stoptime')->select();
            //地址
            $url=$domain."/upload/message/";
            foreach ($list as $key=>$value)
            {
                if($value['gflg']=="1")
                {
                    $list[$key]['nickname']='农厂汇官方账号';
                }
                elseif($value['uname']=='')
                {
                    $list[$key]['nickname']=$list[$key]['nickname'];
                }
                else
                {
                    $list[$key]['nickname']=$value['uname'];
                }
                if($value['unit']=="1")
                {
                    $list[$key]['unit']='吨';
                }
                elseif($value['unit']=="2")
                {
                    $list[$key]['unit']='斤';
                }
                elseif($value['unit']=="3")
                {
                    $list[$key]['unit']='斤';
                }
                else
                {
                    $list[$key]['unit']='个';
                }
                $a=$list[$key]['picture'];
                $arr=array_filter(explode(',',$a));
                //print_r($arr);die;
                //数组非空设置追加路径
                if(!empty($arr))
                {
                    $list[$key]['picture']=$url.$arr[0];
                }
                else
                {
                    $list[$key]['picture']=$url.'no.jpg';
                }
            }
            return $this->AjaxReturn("1","操作成功",$list);
        }
        else
        {
            return $this->AjaxReturn("-1","暂无信息");
        }
    }

    //点赞添加的信息
    public function dianzan()
    {
        $openid = $this->uid;//openid
      	$name = db('user')->where('uid',$openid)->find();
      	$name['uname'] = $name['uname']==''? $name['nickname']:$name['uname'];
        $mid = input('mid');//接收文章mid
        $uflg=db('user')->where('uid',$openid)->value('uflg');
        if($uflg!=0)
        {
            if(!empty($mid))
            {
                $cha = db('message')->where('mid', $mid)->field('id')->find();
                if ($cha['id'] == '') {
                    $str = $openid . ',';
                    $cha = db('message')->where('mid', $mid)->update(['id' => $str]);
                    $time = date('Y-m-d H:i:s');
                    $rs = db('praise')->insert(['mid' => $mid, 'uid' => $openid, 'createtime' => $time]);
                    if ($cha) {
                        return $this->AjaxReturn("1", "点赞成功！",$name['uname']);
                    }
                } else {
                    $id = array_filter(explode(',', $cha['id']));
                    if (in_array($openid, $id)) {
                        return $this->AjaxReturn("-1", "该文章你已经点赞过！");
                    } else {
                        $str = $cha['id'] . $openid . ',';
                        $cha = db('message')->where('mid', $mid)->update(['id' => $str]);
                        $time = date('Y-m-d H:i:s');
                        $rs = db('praise')->insert(['mid' => $mid, 'uid' => $openid, 'createtime' => $time]);
                        if ($cha) {
                            return $this->AjaxReturn("1", "点赞成功！",$name['uname']);
                        }
                    }
                }
            }
            else
            {
                return $this->AjaxReturn("-1","未接收到文章mid！");
            }
        }
        else
        {
            return $this->AjaxReturn("-10","黑名单状态无权操作！");
        }

    }

    //取消点赞的操作
    public function quxiao()
    {
        $openid = $this->uid;//openid
      	$name = db('user')->where('uid',$openid)->find();
      	$name['uname'] = $name['uname']==''? $name['nickname']:$name['uname'];
        $mid = input('mid');//接收文章mid
        $uflg=db('user')->where('uid',$openid)->value('uflg');
        if($uflg!=0)
        {
            if(!empty($mid))
            {
                $cha = db('message')->where('mid', $mid)->field('id')->find();
                $id = array_filter(explode(',', $cha['id']));
                if (!in_array($openid, $id)) {
                    return $this->AjaxReturn("-1", "该文章你未点过赞！");
                } else {
                    foreach ($id as $key=>$value)
                    {
                        if($openid == $value)
                        {
                            unset($id[$key]);
                        }
                    }
                    //最后一个取消关注的要删除逗号
                    if(count($id)=='')
                    {
                        //return 1;die;
                        $str=implode(',',$id);
                    }
                    else
                    {
                        //return 2;die;
                        $str=implode(',',$id).',';
                    }
                    $cha = db('message')->where('mid', $mid)->update(['id' => $str]);
                    $rs = db('praise')->where('mid',$mid)->where('uid',$openid)->delete();
                    if ($cha && $rs) {
                        return $this->AjaxReturn("1", "取消点赞成功！",$name['uname']);
                    }
                }
            }
            else
            {
                return $this->AjaxReturn("-1","未接收到文章mid！");
            }
        }
        else
        {
            return $this->AjaxReturn("-10","黑名单状态无权操作！");
        }

    }

    //一级评论的添加
    public function pinglunad()
    {
        $openid = $this->uid;//openid
        $mid = input('mid');//接收文章mid
      	$content=input('content');//评价内容
        $formid = input('formid');//formid
        $user = db('message')->where('mid',$mid)->field('uid')->find();
        $use = db('user')->where('uid',$user['uid'])->field('code')->find();
        $uses = db('user')->where('uid',$openid)->field('uname,phone,nickname')->find();
        if ($uses['uname']==''){
            $name = $uses['nickname'];
        }else{
            $name = $uses['uname'];

        }
        $mes = new Message();
        
        $uflg=db('user')->where('uid',$openid)->value('uflg');
        if($uflg!=0)
        {
            //去掉表情
            if($content=='')
            {
                return $this->AjaxReturn("-2", "评论内容不支持表情！");
            }
            else
            {
                if(!empty($mid))
                {
                    $time=date('Y-m-d H:i:s');
                    $add=db('evalute')->insert(['message_id'=>$mid,'pjid'=>$openid,'bpid'=>0,'parent_evalute_id'=>0,'create_time'=>$time,'txt'=>$content,'sfig'=>1]);
                 	
                    if ($add) {
                      	$mes->index($use['code'],$name,$uses['phone'],$content,$formid);
                        return $this->AjaxReturn("1", "评论成功！");
                    }
                }
                else
                {
                    return $this->AjaxReturn("-1","未接收到文章mid！");
                }
            }

        }
        else
        {
            return $this->AjaxReturn("-10","黑名单状态无权评论！");
        }


    }

    //二级评论的添加
    public function pinglunadd()
    {
        $openid = $this->uid;//openid
        $mid = input('mid');//接收文章mid
        $content=input('content');//评价内容
        $uflg=db('user')->where('uid',$openid)->value('uflg');
        if($uflg!=0)
        {
            //去掉表情
            if($content=='')
            {
                return $this->AjaxReturn("-2", "评论内容不支持表情！");
            }
            else
            {
                if(!empty($mid))
                {
                    $evalute_id=input('evalute_id');//id上级评论
                    $pjid=db('evalute')->where('evalute_id',$evalute_id)->field('pjid')->find();
                    $time=date('Y-m-d H:i:s');
                    $add=db('evalute')->insert(['message_id'=>$mid,'pjid'=>$openid,'bpid'=>$pjid['pjid'],'parent_evalute_id'=>$evalute_id,'create_time'=>$time,'txt'=>$content,'sfig'=>1]);
                    if ($add) {
                        return $this->AjaxReturn("1", "评论成功！");
                    }
                }
                else
                {
                    return $this->AjaxReturn("-1","未接收到文章mid！");
                }
            }

        }
        else
        {
            return $this->AjaxReturn("-10","黑名单状态无权评论！");
        }

    }

    //拨号的添加
    public function bohao()
    {
        $openid = $this->uid;//openid
        $mid = input('mid');//接收文章mid
        $uflg=db('user')->where('uid',$openid)->value('uflg');
        if($uflg!=0)
        {
            if(!empty($mid))
            {
                $bjid=db('message')->where('mid',$mid)->field('uid')->find();
                //判断是否认证&是否是官方账号
                $rflg=db('real')->join('f_user us','us.uid=f_real.uid')->where('f_real.uid',$openid)->field('f_real.rflg,us.gflg')->find();
                //查询用户或者会员拨号设置的次数
                $num=db('shezhi')->where('zid',1)->field('userwenzhang,vipwenzhang,userbohao,vipbohao')->find();
                $timestart=date("Y-m-d 00:00:00");
                $timeend=date("Y-m-d H:i:s");
                $shu=db('bohao')
                    ->where('zjid',$openid)
                    ->where('f_bohao.b_createtime','between',[$timestart,$timeend])
                    ->count();//今日拨号
                if($rflg['gflg']==1)
                {
                    $time=date('Y-m-d H:i:s');
                    $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$bjid['uid'],'b_createtime'=>$time]);
                    if ($add) {
                        return $this->AjaxReturn("1", "拨号成功！");
                    }
                }
                else
                {
                    if($rflg['rflg']==1)
                    {
                        if($num['vipbohao']>$shu)
                        {
                            $time=date('Y-m-d H:i:s');
                            $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$bjid['uid'],'b_createtime'=>$time]);
                            if ($add) {
                                return $this->AjaxReturn("1", "拨号成功！");
                            }
                        }
                        else
                        {
                            return $this->AjaxReturn("-3","你今日限拨次数已用完！");
                        }
                    }
                    else
                    {
                        if($num['userbohao']>$shu)
                        {
                            $time=date('Y-m-d H:i:s');
                            $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$bjid['uid'],'b_createtime'=>$time]);
                            if ($add) {
                                return $this->AjaxReturn("1", "拨号成功！");
                            }
                        }
                        else
                        {
                            return $this->AjaxReturn("-3","你今日限拨次数已用完！");
                        }
                    }
                }


            }
            else
            {
                return $this->AjaxReturn("-1","未接收到文章mid！");
            }
        }
        else
        {
            return $this->AjaxReturn("-10","黑名单状态无权拨号！");
        }

    }

    //用户主页拨号
    public function user_bohao()
    {
        $openid = $this->uid;//openid
        $uid = input('uid');//接收被转发人uid
        $uflg=db('user')->where('uid',$openid)->value('uflg');
        if($uflg!=0)
        {
            if(!empty($uid))
            {
                //判断是否认证&是否是官方账号
                $rflg=db('real')->join('f_user us','us.uid=f_real.uid')->where('f_real.uid',$uid)->field('f_real.rflg,us.gflg')->find();
                //查询用户或者会员拨号设置的次数
                $num=db('shezhi')->where('zid',1)->field('userwenzhang,vipwenzhang,userbohao,vipbohao')->find();
                $timestart=date("Y-m-d 00:00:00");
                $timeend=date("Y-m-d H:i:s");
                $shu=db('bohao')
                    ->where('zjid',$openid)
                    ->where('f_bohao.b_createtime','between',[$timestart,$timeend])
                    ->count();//今日拨号
                if($rflg['gflg']==1)
                {
                    $time=date('Y-m-d H:i:s');
                    $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$uid,'b_createtime'=>$time]);
                    if ($add) {
                        return $this->AjaxReturn("1", "拨号成功！");
                    }
                }
                else
                {
                    if($rflg['rflg']==1)
                    {
                        if($num['vipbohao']>$shu)
                        {
                            $time=date('Y-m-d H:i:s');
                            $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$uid,'b_createtime'=>$time]);
                            if ($add) {
                                return $this->AjaxReturn("1", "拨号成功！");
                            }
                        }
                        else
                        {
                            return $this->AjaxReturn("-3","你今日限拨次数已用完！");
                        }
                    }
                    else
                    {
                        if($num['userbohao']>$shu)
                        {
                            $time=date('Y-m-d H:i:s');
                            $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$uid,'b_createtime'=>$time]);
                            if ($add) {
                                return $this->AjaxReturn("1", "拨号成功！");
                            }
                        }
                        else
                        {
                            return $this->AjaxReturn("-3","你今日限拨次数已用完！");
                        }
                    }
                }


            }
            else
            {
                return $this->AjaxReturn("-1","未接收到被转发人uid！");
            }
        }
        else
        {
            return $this->AjaxReturn("-10","黑名单状态无权拨号！");
        }


    }

    //用户主页分享名片
    public function user_share()
    {
        $openid = $this->uid;//openid
        $uid = input('uid');//接收uid
        $time=date('Y-m-d H:i:s');
        $add=db('share')->insert(['zid'=>$openid,'bid'=>$uid,'createtime'=>$time]);
        if ($add) {
            return $this->AjaxReturn("1", "分享成功！");
        }
    }

    //转发的添加
    public function zhuanfa()
    {
        $openid = $this->uid;//openid
        $mid = input('mid');//接收文章mid
        if(!empty($mid))
        {
            $bjid=db('message')->where('mid',$mid)->field('f_message.uid')->find();
            //判断是否认证&是否是官方账号
            $rflg=db('real')->join('f_user us','us.uid=f_real.uid')->where('f_real.uid',$openid)->field('f_real.rflg,us.gflg')->find();
            //查询用户或者会员拨号设置的次数
            $num=db('shezhi')->where('zid',1)->field('userwenzhang,vipwenzhang,userbohao,vipbohao,userzhuanfa,vipzhuanfa')->find();
            $timestart=date("Y-m-d 00:00:00");
            $timeend=date("Y-m-d H:i:s");
            $shu=db('zhuanfa')
                ->where('zfid',$openid)
                ->where('f_zhuanfa.z_createtime','between',[$timestart,$timeend])
                ->count();//今日拨号
            //官方账号无限制
            if($rflg['gflg']==1)
            {
                $time=date('Y-m-d H:i:s');
                $add=db('zhuanfa')->insert(['zfid'=>$openid,'fbid'=>$bjid['uid'],'z_createtime'=>$time,'mid'=>$mid]);
                if ($add) {
                    return $this->AjaxReturn("1", "转发成功！");
                }
            }
            else
            {
                if($rflg['rflg']==1)
                {
                    if($num['vipzhuanfa']>$shu)
                    {
                        $time=date('Y-m-d H:i:s');
                        $add=db('zhuanfa')->insert(['zfid'=>$openid,'fbid'=>$bjid['uid'],'z_createtime'=>$time,'mid'=>$mid]);
                        if ($add) {
                            return $this->AjaxReturn("1", "转发成功！");
                        }
                    }
                    else
                    {
                        return $this->AjaxReturn("-3","你今日限转发次数已用完！");
                    }
                }
                else
                {
                    if($num['userzhuanfa']>$shu)
                    {
                        $time=date('Y-m-d H:i:s');
                        $add=db('zhuanfa')->insert(['zfid'=>$openid,'fbid'=>$bjid['uid'],'z_createtime'=>$time,'mid'=>$mid]);
                        if ($add) {
                            return $this->AjaxReturn("1", "转发成功！");
                        }
                    }
                    else
                    {
                        return $this->AjaxReturn("-3","你今日限转发次数已用完！");
                    }
                }
            }
        }
        else
        {
            return $this->AjaxReturn("-1","未接收到文章mid！");
        }

    }

}
